﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Read.Api.Application.Command;
using B.S.BaseData.Read.Api.Dtos;
using MediatR;
using SqlSugar;

namespace B.S.BaseData.Read.Api.Application.CommandHander
{
    public class GetUserHander : IRequestHandler<GetUserCommand, ApiResult<List<UserDto>>>
    {

        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public GetUserHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        /// <summary>
        /// 用户显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<List<UserDto>>> Handle(GetUserCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<UserDto>> result = new ApiResult<List<UserDto>>();

            var entity = db.Queryable<User>().Where(x => x.UserName != "admin");

            result.Code = ApiEnum.显示成功;
            result.Msg = ApiEnum.显示成功.ToString();
            result.Data = mapper.Map<List<UserDto>>(entity.ToList());

            return Task.FromResult(result);
        }
    }
}
